public class isBalanced {

    class Solution {
        public boolean isBalanced(TreeNode root) {
            if(root == null) return true;
            if(getHength(root) > 0) {
                return true;
            }else {
                return false;
            }
        }

        public int getHength(TreeNode root) {
            if(root == null) return 0;

            int ret = getHength(root.left);
            if(ret < 0) {
                return -1;
            }
            int ret2 = getHength(root.right);

            if(ret2 >= 0&&Math.abs(ret - ret2) <= 1) {
                return Math.max(ret,ret2) + 1;
            }else {
                return -1;
            }
        }
    }
    public static void main(String[] args) {

    }
}
